package shop.core.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description 字符串工具类
 * @Author semstouch
 * @Date 2017/4/7
 **/
public class StrUtils {
    protected static final Logger logger = LoggerFactory.getLogger(StrUtils.class);

    public static String getUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }
    public static Long getNowTime(){
        Date date = new Date(System.currentTimeMillis());
        return  date.getTime();
    }

    /**
     * 获取现在时间
     *
     * @return 返回短时间字符串格式yyyy-MM-dd
     */
    public static String getStringMonthShort() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * 获取现在时间
     * @return 返回短时间字符串格式yyyyMMdd
     */
    public static String getStringToday() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * 获取昨日起始时间
     * @return
     */
    public static Long yesterdayStartTime(){
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        String yesterday = new SimpleDateFormat( "yyyy-MM-dd").format(cal.getTime());
        String yesterdayBegin = yesterday + " 00:00:00";
        try
        {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sdf.parse(yesterdayBegin);
            return date.getTime();
        }
        catch (ParseException e)
        {
            System.out.println("时间获取失败"+e.getMessage());
        }
        return null;
    }

    /**
     * 获取昨日结束时间
     * @return
     */
    public static Long yesterdayEndTime(){
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        String yesterday = new SimpleDateFormat( "yyyy-MM-dd").format(cal.getTime());
        String yesterdayBegin = yesterday + " 23:59:59";
        try
        {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sdf.parse(yesterdayBegin);
            return date.getTime();
        }
        catch (ParseException e)
        {
            System.out.println("时间获取失败"+e.getMessage());
        }
        return null;
    }

    /**
     *将emoji标签转换成utf8字符集保存进数据库
     *@param str 需要转换的字符串
     *@return
     */
    public static String emojiConvert(String str) {

        String patternString = "([\\x{10000}-\\x{10ffff}\ud800-\udfff])";

        Pattern pattern = Pattern.compile(patternString);
        Matcher matcher = pattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while(matcher.find()) {
            try {
                matcher.appendReplacement(sb,"[[" + URLEncoder.encode(matcher.group(1), "UTF-8") + "]]");
            } catch(UnsupportedEncodingException e) {
                logger.error("emojiConvert error"+ e.getMessage());
                return str;
            }
        }
        matcher.appendTail(sb);
        logger.debug("emojiConvert " + str + " to " + sb.toString() + ", len：" + sb.length());
        return sb.toString();
    }

    /**
     * @Description 还原utf8数据库中保存的含转换后emoji表情的字符串
     * @param str
     * 转换后的字符串
     * @return 转换前的字符串
     */
    public static String emojiRecovery(String str) {
        String patternString = "\\[\\[(.*?)\\]\\]";

        Pattern pattern = Pattern.compile(patternString);
        Matcher matcher = pattern.matcher(str);

        StringBuffer sb = new StringBuffer();
        while(matcher.find()) {
            try {
                matcher.appendReplacement(sb,
                        URLDecoder.decode(matcher.group(1), "UTF-8"));
            } catch(UnsupportedEncodingException e) {
                logger.error("emojiRecovery error"+ e.getMessage());
                return "";
            }
        }
        matcher.appendTail(sb);
        return sb.toString();
    }




    public static void main(String[] args) throws ParseException {
        DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = dateFormat2.parse("2017-04-11 00:00:00");
        System.out.println(date.getTime());

        System.out.println(yesterdayStartTime());

    }
}
